﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class UnitAbbreviationControl : UserControl, IControlRulable
    {
        private UnitAbbreviation innerUnitAbbreviation;

        public UnitAbbreviation InnerUnitAbbreviation
        {
            get { return innerUnitAbbreviation; }
            set { innerUnitAbbreviation = value; }
        }

        public UnitAbbreviationControl()
        {
            InitializeComponent();
            this.innerUnitAbbreviation = new UnitAbbreviation();
            this.FillControls();
        }

        public UnitAbbreviationControl(UnitAbbreviation ua):this()
        {
            this.innerUnitAbbreviation = ua;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.icSequence.Value = this.innerUnitAbbreviation.Sequence;
                this.strValue.Value = this.innerUnitAbbreviation.Value;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerUnitAbbreviation.Sequence = this.icSequence.Value;
                    this.innerUnitAbbreviation.Value = this.strValue.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UnitAbbreviationControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerUnitAbbreviation.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
